<?php

class ShopMode extends SiteMode {
    /** @return ShopMode */
    public static function init() {
        return parent::init();
    }
    public function __construct(){
        parent::__construct();
        $this->user=UserModule::init('user');
    }
    //搜索商家的信息 TODO 图片目前只搜了id
    function getDetail($id){
        $sql="SELECT * FROM `shop_info` WHERE `id`=? and site_id=?";
        return $this->db->getOne($sql,$id, $this->siteId);
    }
    //TODO  条件搜索
    function getList($enable='all',$rows = 30,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        switch($enable){
            case 0:$cond=" `enable`=0 ";break;
            case 1:$cond=" `enable`=1 ";break;
            default:$cond=" 1 ";
        }
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `shop_info` WHERE $cond and site_id=? LIMIT $offset,$rows";
        return $this->db->getList($sql, $this->siteId);
    }
    //检查该用户是否已经申请过
    function checkExist($user_id){
        $sql="SELECT 1 FROM `shop_info` WHERE `user_id`=? and site_id=?";
        return $this->db->getExist($sql,$user_id, $this->siteId);
    }
    //一长串要添加的信息
    function add($user_id, $real_name, $sex='male',$tel, $academic,$birth, $idnum, $idnum_pic,
                 $company, $license_pic, $company_pic, $nation=null,$qq=null, $email=null,
                 $address=null, $computer_skill=null, $recent_pic=null, $introduction=null,
                 $address_id=null, $address_text=null, $visible=0, $longitude=null, $latitude=null){
        $data['user_id']=$user_id;
        $data['real_name']=$real_name;
        $data['sex']=$sex;
        $data['birth']=$birth;
        $data['nation']=$nation;
        $data['tel']=$tel;
        $data['qq']=$qq;
        $data['email']=$email;
        $data['academic']=$academic;
        $data['address']=$address;
        $data['computer_skill']=$computer_skill;
        $data['idnum']=$idnum;
        $data['idnum_pic']=$idnum_pic;
        $data['recent_pic']=$recent_pic;
        $data['license_pic']=$license_pic;
        $data['company']=$company;
        $data['company_pic']=$company_pic;
        $data['introduction']=$introduction;
        $data['address_id']=$address_id;
        $data['address_text']=$address_text;
        $data['longitude']=$longitude;
        $data['latitude']=$latitude;
        $data['coordinate_visible']=$visible;
        $data['site_id']=$this->siteId;
        if($this->db->insert('shop_info',$data,true)){
            $type=$this->user->getType($user_id);
            if($type == 'service'){
                $this->user->setType($user_id,'both');
            }else{
                $this->user->setType($user_id,'shop');
            }
            return (int)$this->db->insertId();
        }
        return -1;
    }
    //太多了 TODO 要不要分开？
    function modify($id, $real_name, $sex='male',$tel, $academic,$birth, $idnum, $idnum_pic,
                    $company, $license_pic, $company_pic, $nation=null,$qq=null, $email=null,
                    $address=null, $computer_skill=null, $recent_pic=null, $introduction=null,
                    $address_id=null, $address_text=null, $visible=0, $longitude=null, $latitude=null){
        $id=(int)$id;
        $data['real_name']=$real_name;
        $data['sex']=$sex;
        $data['birth']=$birth;
        $data['nation']=$nation;
        $data['tel']=$tel;
        $data['qq']=$qq;
        $data['email']=$email;
        $data['academic']=$academic;
        $data['address']=$address;
        $data['computer_skill']=$computer_skill;
        $data['idnum']=$idnum;
        $data['idnum_pic']=$idnum_pic;
        $data['recent_pic']=$recent_pic;
        $data['license_pic']=$license_pic;
        $data['company']=$company;
        $data['company_pic']=$company_pic;
        $data['introduction']=$introduction;
        $data['address_id']=$address_id;
        $data['address_text']=$address_text;
        $data['longitude']=$longitude;
        $data['latitude']=$latitude;
        $data['latitude']=$latitude;
        $data['coordinate_visible']=$visible;
        $data['site_id']=$this->siteId;
        return $this->db->update('shop_info',$id,$data,true) == 1;
    }
    //修改商店坐标,商店可以选择显示或者不显示
    function modifyCoordinate($id,$longitude, $latitude,$visible = 0){
        $data['longitude']=$longitude;
        $data['latitude']=$latitude;
        $data['coordinate_visible']=$visible;
        return $this->db->update('shop_info',$id,$data,true) == 1;
    }
    //单纯的删除一条shop_info
    function delete($id){
        return $this->db->delete('shop_info',$id) == 1;
    }
    /**
     * 审核
     * @param $id
     * @param int $authPass 0|1
     * @return bool
     */
    function auth($id,$authPass = 1){
        $data['enable']=$authPass;
        return $this->db->update('shop_info',$id,$data,true) == 1;
    }
    function getShopId($user_id){
        $sql="SELECT `id` FROM `shop_info` WHERE `user_id`=? and site_id=?";
        return $this->db->getValue($sql,$user_id, $this->siteId);
    }
    //根据商家名称获得商家id
    function getShopIdByCompany($company){
        $company=$this->db->quote("%{$company}%");
        $sql="SELECT `id` FROM `shop_info` WHERE site_id=? and `company` like ".$company;
        return $this->db->getValue($sql, $this->siteId);
    }
    function getUserId($id){
        $sql="SELECT `user_id` FROM `shop_info` WHERE `id`=? and site_id=?";
        return $this->db->getValue($sql,$id, $this->siteId);
    }
    function getShopInfoBySearch($company,$real_name,$tel){
        $condition='';
        if(!empty($company)){
            $condition.=" AND `company` like".$this->db->quote("%{$company}%");
        }
        if(!empty($tel)){
            $condition.=" AND `tel` like".$this->db->quote("%{$tel}%");
        }
        if(!empty($real_name)){
            $condition.=" AND `real_name` like ".$this->db->quote("%{$real_name}%");
        }
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `shop_info` WHERE 1 $condition and site_id=?
              ORDER BY `create_time` DESC ";
        return $this->db->getAll($sql, $this->siteId);
    }
} 